import { forEach as _forEach } from 'lodash-es';
import {
    UnitSelectionData,
    UserRoleUnitSelectionServiceProxy
} from '@shared/service-proxies/service-proxies';
import { Injectable } from '@angular/core';

@Injectable()
export class AppUserRoleUnitService extends UserRoleUnitSelectionServiceProxy{

    getUnitFlatDataForSelection(filterFunc: { (data: UnitSelectionData[]): UnitSelectionData[] }): Promise<UnitSelectionData[]> {
        return new Promise<UnitSelectionData[]>((resolve, reject) => {
            this.getAllOrganizationUnitsForSelection().toPromise().then((result: UnitSelectionData[]) => {
                let items = result;
                _forEach(items, item => {
                    item.displayName = Array(item.level).join('---') + ' ' + item.displayName;
                });

                if (filterFunc) {
                    filterFunc(items);
                }

                resolve(items);
            }, (err) => {
                reject && reject(err);
            });
        });
    }
}
